﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Caliburn.Micro;

namespace DataEditor.ViewModels
{
    public class IndicatorsViewModel : BaseViewModel
    {
        readonly IWindowManager _windowManager;
        readonly IEventAggregator _eventAggregator;

        #region Constructors

        public IndicatorsViewModel(IEventAggregator eventAggregator, IWindowManager windowManager)
        {
            _windowManager = windowManager;
            _eventAggregator = eventAggregator;
            MakeCommands();
        }

        #endregion


        #region Properties

        ObservableCollection<IndicatorViewModel> _indicators;
        public ObservableCollection<IndicatorViewModel> Indicators
        {
            get
            {
                if (_indicators == null)
                {
                    _indicators = new ObservableCollection<IndicatorViewModel>();
                    foreach (var item in dataContext.INDICATORs)
                    {
                        _indicators.Add(new IndicatorViewModel(item));
                    }
                }

                return _indicators;
            }
        }

        public IndicatorViewModel SelectedIndicator { get; set; }

        private RelayCommand _addNewCommand;
        public RelayCommand AddNewCommand
        {
            get { return _addNewCommand; }
            set { _addNewCommand = value; }
        }

        private RelayCommand _removeCommand;
        public RelayCommand RemoveCommand
        {
            get { return _removeCommand; }
            set { _removeCommand = value; }
        }

        private RelayCommand _editCommand;
        public RelayCommand EditCommand
        {
            get { return _editCommand; }
            set { _editCommand = value; }
        }

        #endregion

        #region Actions/methods

        private void MakeCommands()
        {
            _addNewCommand = new RelayCommand(
                param =>
                {
                    var newIndicator = new DataLayer.INDICATOR
                    {
                        INDICATOR_TYPE_FK = 1,
                        NAME = "",
                        DESCRIPTION="",
                        IsValid = true,
                        ValidFrom = DateTime.Now
                    };
                    dataContext.INDICATORs.AddObject(newIndicator);
                    dataContext.SaveChanges();
                    Indicators.Add(new IndicatorViewModel(newIndicator));
                    NotifyOfPropertyChange(() => Indicators);
                },
                (param) =>
                {
                    return true;
                }
                );
            _removeCommand = new RelayCommand(
                param =>
                {
                    dataContext.INDICATORs.DeleteObject(SelectedIndicator.Model);
                    dataContext.SaveChanges();
                    Indicators.Remove(SelectedIndicator);
                    NotifyOfPropertyChange(() => Indicators);
                },
                (param) =>
                {
                    return SelectedIndicator != null;
                }
                );

            _editCommand = new RelayCommand(
                param =>
                {
                    EditIndicator(this.SelectedIndicator);
                },
                (param) =>
                {
                    return SelectedIndicator != null;
                }
                );
        }

        public void MouseDoubleClick(object dataContext, EventArgs e)
        {
            IndicatorViewModel vmDataContext = (dataContext as IndicatorsViewModel).SelectedIndicator;
            EditIndicator(vmDataContext);
        }

        private void EditIndicator(IndicatorViewModel indicatorViewModel)
        {
            Nullable<Boolean> result = _windowManager.ShowDialog(indicatorViewModel);
        }

        #endregion
    }
}
